package com.itheima.run;

import com.itheima.bean.Business;
import com.itheima.bean.Customer;
import com.itheima.bean.Movie;
import com.itheima.bean.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class MovieSystem {
    private static final Logger LOGGER = LoggerFactory.getLogger("MovieSystem");

    public static final List<User> ALL_USERS = new ArrayList<>();

    public static final Map<Business, List<Movie>> ALL_MOVIES = new HashMap<>();

    public static final Scanner SC = new Scanner(System.in);

    static {
        //客户数据
        Customer c = new Customer();
        c.setLoginName("ldh888");
        c.setPassWord("123456");
        c.setUserName("黑马刘德华");
        c.setSex('男');
        c.setMoney(10000);
        c.setPhone("110110");
        ALL_USERS.add(c);

        Customer c1 = new Customer();
        c1.setLoginName("gzl888");
        c1.setPassWord("123456");
        c1.setUserName("黑马关之琳");
        c1.setSex('女');
        c1.setMoney(2000);
        c1.setPhone("111111");
        ALL_USERS.add(c1);

        //商家数据
        Business b = new Business();
        b.setLoginName("baozugong888");
        b.setPassWord("123456");
        b.setUserName("黑马包租公");
        b.setMoney(0);
        b.setSex('男');
        b.setPhone("110110");
        b.setAddress("火星6号2B二层");
        b.setShopName("甜甜圈国际影城");
        ALL_USERS.add(b);

        List<Movie> movies = new ArrayList<>();
        Movie movie1 = new Movie();
        movie1.setName("七品芝麻官");
        movie1.setPrice(200);
        movie1.setTime(100);
        movies.add(movie1);
        movie1.setNumber(10);
        ALL_MOVIES.put(b, movies);

        Business b2 = new Business();
        b2.setLoginName("baozupo888");
        b2.setPassWord("123456");
        b2.setUserName("黑马包租婆");
        b2.setMoney(0);
        b2.setSex('女');
        b2.setPhone("110110");
        b2.setAddress("火星8号8B八层");
        b2.setShopName("巧克力国际影城");
        ALL_USERS.add(b2);

        List<Movie> movies3 = new ArrayList<>();
        ALL_MOVIES.put(b2, movies3);
    }

    public static void main(String[] args) {
        shoMain();
    }

    public static void shoMain() {
        while (true) {
            System.out.println("=====================黑马电影首页=====================");
            System.out.println("1、登录");
            System.out.println("2、用户注册");
            System.out.println("3、商家注册");
            System.out.println("请输入操作命令：");

            String choice = SC.nextLine();

            switch (choice) {
                case "1":
                    login();
                    break;
                case "2":
                    System.out.println("待实现...");
                    break;
                case "3":
                    System.out.println("待实现...");
                    break;
                default:
                    System.out.println("命令有误，请确认！");
                    break;
            }
        }
    }

    public static void login() {
        while (true) {
            System.out.println("请您输入登录名称：");
            String loginName = SC.nextLine();
            System.out.println("请您输入登录密码：");
            String passWord = SC.nextLine();

            User u = getUserByLoginName(loginName);
            if (u != null) {
                if (u.getPassWord().equals(passWord)) {
                    System.out.println(u.getUserName() + "登陆了系统~~~");
                    if (u instanceof Customer) {
                        showCustomerMain(u);
                    } else {
                        showBusinessMain(u);
                    }
                    return;
                } else {
                    System.out.println("密码有误，请重新输入...");
                }
            } else {
                System.out.println("登录名称错误，请重新输入...");
            }
        }
    }

    public static void showBusinessMain(User loginUser) {
        while (true) {
            System.out.println("=====================商家系统=====================");
            System.out.println(loginUser.getUserName() + (loginUser.getSex() == '男' ? "先生" : "女士" + "欢迎您进入系统"));
            System.out.println("1、展示详情：");
            System.out.println("2、上架电影：");
            System.out.println("3、下架电影：");
            System.out.println("4、修改电影：");
            System.out.println("5、退出：");

            System.out.println("请输入您要操作的命令：");
            String command = SC.nextLine();
            switch (command) {
                case "1":
                    showBusinessInfo(loginUser);
                    break;
                case "2":
                    addMovie(loginUser);
                    break;
                case "3":
                    removeMovie(loginUser);
                    break;
                case "4":
                    updateMovie(loginUser);
                    break;
                default:
                    System.out.println("录入指令有误，请重新选择...");
                    break;
            }
        }
    }

    public static void updateMovie(User loginUser) {
        //1、根据登录的商家，获取商家下的电影列表(List<Movie>集合)
        List<Movie> movies = ALL_MOVIES.get(loginUser);

        //2、录入：要修改的电影名称
        System.out.println("请录入要修改的电影名称：");
        String movieName = SC.nextLine();

        //3、根据录入的电影名称 ， 遍历List<Movie>集合，找到相应电影名称的 Movie对象
        Movie m = null;
        for (Movie movie : movies) {
            if (movie.getName().equals(movieName)) {
                m = movie;
            }
        }

        //4、判断：Movie对象是否为null   （没有这个电影：提示      有：可以修改了）
        if (m == null) {
            System.out.println("没有这个电影！！");
            return;
        }

        //5、录入：新的电影数据
        //6、修改Movie对象中的属性（把录入新数据覆盖旧数据）
        System.out.println("请输入新的电影名称：");
        String newMovieName = SC.nextLine();
        m.setName(newMovieName);

        System.out.println("请输入新的电影时长：");
        int newTime = SC.nextInt();
        m.setTime(newTime);

        System.out.println("请输入新的电影主演：");
        String newActor = SC.nextLine();
        m.setActor(newActor);

        System.out.println("请输入新的电影票价：");
        int newPrice = SC.nextInt();
        m.setPrice(newPrice);

        System.out.println("恭喜您，您成功修改了该影片！！！");
    }

    public static void removeMovie(User loginUser) {
        System.out.println("请您输入需要下架的电影名称：");
        String movieName = SC.nextLine();

        //1、根据登录的商家，获取商家下的电影列表(List<Movie>集合)
        List<Movie> movies = ALL_MOVIES.get(loginUser);

        //2、遍历List<Movie>集合，查找指定电影名称的Movie对象在集合中的索引
        //3、没有找到匹配的电影 ：  索引 == -1
        int index = -1;
        for (Movie movie : movies) {
            index++;
            if (movie.getName().equals(movieName)) {
                break;
            }
        }
        //提示

        //4、找到匹配的电影：     有索引值
        //从List<Movie>集合中，删除指定索引下的元素
        //Object  remove(int index) :  根据索引删除集合中的元素后，会返回被删除的元素
        if (index != -1) {
            movies.remove(index);
            System.out.println("恭喜您，您成功下架了该影片！！！");
        }
    }

    public static void addMovie(User loginUser) {
        //1、根据登录的商家，获取商家下的电影列表
        //Map<商家, 电影列表> ALL_MOVIES = new HashMap<>();
        List<Movie> movies = ALL_MOVIES.get(loginUser);

        //2、录入：电影数据信息
        System.out.println("请输入您要上架的电影名称：");
        String movieName = SC.nextLine();
        System.out.println("请输入您要上架的电影主演：");
        String movieActor = SC.nextLine();
        System.out.println("请输入您要上架的电影时长：");
        int movieTime = SC.nextInt();
        System.out.println("请输入您要上架的电影票价：");
        int moviePrice = SC.nextInt();

        //3、把录入的电影信息封装到Movie对象中

        Movie movie = new Movie();
        movie.setName(movieName);
        movie.setActor(movieActor);
        movie.setTime(movieTime);
        movie.setPrice(moviePrice);
        //...

        //4、把Movie对象，存储到：商家的电影列表下
        movies.add(movie);

        System.out.println("您已经成功上架了：《" + movie.getName() + "》");
    }

    public static void showBusinessInfo(User loginUser) {
        System.out.println("=====================商家详情=====================");
        LOGGER.info(loginUser.getUserName() + "商家，正在看自己的详情~~");

        System.out.println(loginUser.getUserName() + "\t\t");
        System.out.println("电话：" + loginUser.getPhone() + "\t\t");

        Business bussiness = (Business) loginUser;
        System.out.println("地址：" + bussiness.getAddress());

        System.out.println("片名\t\t主演\t\t时长\t\t评分\t\t票价\t\t余票数量\t\t放映时间");
        List<Movie> movies = ALL_MOVIES.get(bussiness);
        LOGGER.trace(String.valueOf(movies));

        for (Movie movie : movies) {
            System.out.println(movie.getName() + "\t");
            System.out.println(movie.getActor() + "\t");
            System.out.println(movie.getTime() + "\t");
            System.out.println(movie.getName() + "\t");
            System.out.println(movie.getPrice() + "\t");
            System.out.println(movie.getNumber() + "\t");
            System.out.println(movie.getStartTime());
        }
    }

    public static void showCustomerMain(User loginUser) {
        while (true) {
            System.out.println("=====================黑马电影客户界面======================");
            System.out.println(loginUser.getUserName() + (loginUser.getSex() == '男' ? "先生" : "女士" + "欢迎您进入系统"
                    + "\t余额：" + loginUser.getMoney()));
            System.out.println("请您选择要操作的功能：");
            System.out.println("1、展示全部影片信息功能：");
            System.out.println("2、根据电影名称查询电影信息：");
            System.out.println("3、评分功能：");
            System.out.println("4、购票功能：");
            System.out.println("5、退出系统：");
            System.out.println("请输入您要操作的命令：");
            String command = SC.nextLine();
            switch (command) {
                case "1":
                    showAllMovie();
                    break;
                case "2":
                    // 根据电影名称查询电影信息(选做)
                    break;
                case "3":
                    // 评分功能(扩展功能,选做)
                    break;
                case "4":
                    //TOdo 第六步 :  购票功能
                    buyTicket(loginUser);
                    break;
                case "5":
                    return;
                default:
                    System.out.println("不存在该命令！！");
                    break;
            }
        }
    }

    public static void buyTicket(User loginUser) {
        //1、展示所有商家的影片  （建议：调用方法提供复用性）
        for (Business business : ALL_MOVIES.keySet()) {
            System.out.println("商家：" + business.getUserName() + "\t\t");
        }

        //2、录入：购票需要的基础信息（门店名称）
        System.out.println("请输入商家名称：");
        String storeName = SC.nextLine();
        for (Business business : ALL_MOVIES.keySet()) {
            if (business.getUserName().equals(storeName)) {
                System.out.println("您选择的影院为：" + business.getUserName());
                System.out.println("可购买电影如下：");
                showBusinessInfo(business);
            }
        }
        System.out.println("请输入购买的电影名称：");
        String movieName = SC.nextLine();
        System.out.println("请输入购买的票数：");
        int buyNum = SC.nextInt();

        //   public static final Map<Business, List<Movie>> ALL_MOVIES = new HashMap<>();
        //3、遍历：ALL_MOVIES集合
        //取出每一个商家对象，判断是否有指定名称的商家存在
        //不存在 ： 提示信息
        Movie movie = null;
        Business business = null;
        for (Business b : ALL_MOVIES.keySet()) {
            if (b.getUserName().equals(storeName)) {
                business = b;
                List<Movie> movies = ALL_MOVIES.get(business);
                for (Movie m : movies) {
                    movie = m;
                }
            }
        }
        if (movie != null) {
            if (movie.getNumber() >= buyNum) {
                movie.setNumber(movie.getNumber() - buyNum);
                loginUser.setMoney(loginUser.getMoney() - buyNum * movie.getPrice());
                business.setMoney(business.getMoney() + buyNum * movie.getPrice());
            } else {
                System.out.println("余票不足，请重新购买！");
            }
        } else {
            System.out.println("没有该电影，请重新输入！");
        }

        //4、商家存在，再次录入： 电影名称、 电影票数


        //5、遍历：商家下的List<Movie>集合
        //取出 Movie名字 和 录入的电影名称 比较
        //不存在 ： 提示信息

        //存在：
        //输出信息 （根据电影票价 * 购买票数 = 总金额）
        //更新Movie对象中余票数量：   余票数量 - 购买票数
        //更新会员对象中账户余额 ：   账户余额 - 支付金额
        System.out.println("您的账户余额为：");
        System.out.println(loginUser.getMoney());
    }

    public static void showAllMovie() {
        Set<Map.Entry<Business, List<Movie>>> entries = ALL_MOVIES.entrySet();
        for (Map.Entry<Business, List<Movie>> entry : entries) {
            Business business = entry.getKey();
            System.out.println(business.getUserName() + "\t\t");
            System.out.println("电话：" + business.getPassWord() + "\t\t");
            System.out.println("地址：" + business.getAddress() + "\t\t");

            System.out.println("片名\t\t主演\t\t时长\t\t评分\t\t票价\t\t余票数量\t\t放映时间");

            List<Movie> movies = entry.getValue();

            for (Movie movie : movies) {
                //.........
            }
        }
    }

    public static User getUserByLoginName(String loginName) {
        for (User u : ALL_USERS) {
            if (u.getLoginName().equals(loginName)) {
                return u;
            }
        }
        return null;
    }
}
